//
//  ZLTravelsViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "ZLTravelsViewController.h"
#import "ZLFirstViewController.h"
#import "ZlSecondViewController.h"

@interface ZLTravelsViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong)UISegmentedControl *segmentedC;

@property (nonatomic, strong)ZLFirstViewController *firstVC;
@property (nonatomic, strong)ZlSecondViewController *secondVC;
@property (nonatomic, assign)BOOL isSelect;
@property (nonatomic, strong)UIScrollView *myScrollV;

@end

@implementation ZLTravelsViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    [self createSegment];
    
    self.myScrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.myScrollV.delegate = self;
    self.myScrollV.backgroundColor = [UIColor redColor];
    self.myScrollV.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    self.myScrollV.pagingEnabled = YES;
    self.myScrollV.bounces = NO;
    [self.view addSubview:self.myScrollV];
    
    
    
    
    
    self.firstVC = [ZLFirstViewController new];
    self.firstVC.type = self.type;
    self.firstVC.myId = self.myId;
    
    [self addChildViewController:self.firstVC];
    //    self.firstVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    
    [self.myScrollV addSubview:self.firstVC.view];

    
    self.secondVC = [ZlSecondViewController new];
    self.secondVC.myId = self.myId;
    self.secondVC.type = self.type;
    [self addChildViewController:self.secondVC];
    self.secondVC.view.frame = CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.myScrollV addSubview:self.secondVC.view];

    }
#pragma mark - 创建segment
- (void)createSegment
{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 180 * AUTO_WIDTH)/2, 23, 180*AUTO_WIDTH, 41)];
    myView.backgroundColor = [UIColor colorWithRed:0.33 green:0.74 blue:0.8 alpha:1];

//    [myView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.left.top.right.equalTo(self.view);
//        make.height.equalTo(@64);
//    }];
    NSArray *arr = [NSArray arrayWithObjects:@"游记",@"点评", nil];
    self.segmentedC = [[UISegmentedControl alloc]initWithItems:arr];
    self.segmentedC.tintColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    self.segmentedC.frame = myView.bounds;
    [myView addSubview:self.segmentedC];
    [self.segmentedC addTarget:self action:@selector(segment:) forControlEvents:(UIControlEventValueChanged)];
    self.segmentedC.selectedSegmentIndex = 0;
    
    self.navigationItem.titleView = myView;
}

//segment监听方法
- (void)segment:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        
        self.myScrollV.contentOffset = CGPointMake(0, 0);
        
    }else if(segment.selectedSegmentIndex == 1){
        self.myScrollV.contentOffset = CGPointMake(SCREEN_WIDTH, 0);

    }
//    [self.view bringSubviewToFront:segment];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0) {
        self.segmentedC.selectedSegmentIndex = 0;
        
    }else{
        self.segmentedC.selectedSegmentIndex = 1;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
